﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using hou_elearning.Entity;
using hou_elearning.Business;
using hou_elearning.Custom;

public partial class Controls_ucThongbao : System.Web.UI.UserControl
{
    private List<ThongbaoEntity> glbLstGrvDatasource = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            napGrvThongbao(true);
        }
    }
    /// <summary>
    /// Lấy ID người dùng đăng nhập
    /// </summary>
    /// <author>liemqv</author>
    /// <create>25/04/2010</create>
    /// 

    public Int32 iGetUserLogin()
    {
        Int32 PK_iNguoidungID = 0;
        try
        {
            if (Session["UserID"] != null)
            {
                PK_iNguoidungID = Int32.Parse(Session["UserID"].ToString());
            }
            else
            {
                Response.Redirect("~/Admin/Login.aspx?re=?page=Thongbao");
            }
        }
        catch(Exception ex)
        {
            PK_iNguoidungID = 0;
        }
        return PK_iNguoidungID;
    }

    /// <summary>
    /// Hiển thị danh sách thông báo
    /// </summary>
    /// <author>liemqv</author>
    /// <create>25/04/2012</create>
    public void napGrvThongbao(bool bLoadFromDB)
    {
        if (bLoadFromDB == true || Cache["cacheLstGrvDatasource"] == null)
        {
            glbLstGrvDatasource = new List<ThongbaoEntity>();
            Int64 PK_iNguoidungID = iGetUserLogin();
            List<Thongbao_NguoidungEntity> lstThongbaoNguoidung = Thongbao_NguoidungBRL.GetByFK_iNguoidungID(PK_iNguoidungID);
            
            foreach(Thongbao_NguoidungEntity oThongbaoNguoidung in lstThongbaoNguoidung)
            {
                glbLstGrvDatasource.Add(ThongbaoBRL.GetOne(oThongbaoNguoidung.FK_iThongbaoID));
            }
            lstThongbaoNguoidung = null;
            Cache["cacheLstGrvDatasource"] = glbLstGrvDatasource;
        }
        glbLstGrvDatasource = (List<ThongbaoEntity>)Cache["cacheLstGrvDatasource"];
        if (glbLstGrvDatasource.Count <= 0)
        {
            divThongbao.InnerHtml = "<p class='info' id='warning'><span class='info_inner'>Không có bản ghi nào để hiển thị!</span></p>";
        }
        else
        {
            grvThongbao.DataSource = glbLstGrvDatasource;
            grvThongbao.DataKeyNames = new string[] { "PK_iThongbaoID" };
            grvThongbao.DataBind();
        }
    }

    protected void grvThongbao_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvThongbao.PageIndex = e.NewPageIndex;
        napGrvThongbao(false);
    }
    protected void grvThongbao_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if(e.CommandName == "Sort")
        {
            return;
        }
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow row = grvThongbao.Rows[index];
        Int64 PK_iThongbaoID = Convert.ToInt64(grvThongbao.DataKeys[row.RowIndex].Values["PK_iThongbaoID"]);
        if(e.CommandName == "Dadoc")
        {
            Thongbao_NguoidungEntity oThongbaoNguoidung = Thongbao_NguoidungBRL.GetByFK_iNguoidungIDAndFK_iThongbaoID(iGetUserLogin(), PK_iThongbaoID);
            oThongbaoNguoidung.iTrangthai = 0;
            Thongbao_NguoidungBRL.Edit(oThongbaoNguoidung);
            LinkButton lbtnDadoc = (LinkButton)row.Cells[4].Controls[0];
            lbtnDadoc.Enabled = false;
        }
    }
    protected void grvThongbao_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["SortDirection"] == null)
            ViewState["SortDirection"] = "ASC";
        else
        {
            ViewState["SortDirection"] = ViewState["SortDirection"].ToString() == "ASC" ? "DESC" : "ASC";
        }
        List<ThongbaoEntity> lstKhoahoc = (List<ThongbaoEntity>)Cache["cacheLstGrvDatasource"];
        glbLstGrvDatasource = ThongbaoEntity.Sort(lstKhoahoc, e.SortExpression, ViewState["SortDirection"].ToString());
        napGrvThongbao(false);
    }
    protected void grvThongbao_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Label lblNguoidang = null;
        //Kiểm tra người dùng có trong khóa học hay không
        Int64 PK_iHocvienID = iGetUserLogin();

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            try
            {
                lblNguoidang = (Label)e.Row.FindControl("lblNguoidang");
                Int64 PK_iNguoidungID = Int64.Parse(lblNguoidang.Text);
                lblNguoidang.Text = NguoidungBRL.GetOne(PK_iNguoidungID).sHoten;
            }
            catch (Exception ex)
            {

            }
        }
    }
}